package com.may.wlrj.dao;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.may.wlrj.entity.SysEntity;
import com.may.wlrj.entity.UserEntity;
import com.may.wlrj.util.XinLongUtil;

@Repository
public class SysDAO {
	private Logger log = Logger.getLogger(SysDAO.class);
	private JdbcTemplate jdbcTemplate;

    @Autowired
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
    
    public int updatesys(SysEntity sysEntity) throws EmptyResultDataAccessException{
		// update
		String sql = "UPDATE tbl_sys SET Createtime=?, Descriptor=?, Updatetime=?, Name=?, Sysvalue=? WHERE ID=?";
		try{
			return jdbcTemplate.update(sql, sysEntity.getCreatetime(),sysEntity.getDescriptor(),sysEntity.getUpdatetime(),
					sysEntity.getName(),sysEntity.getSysvalue(),sysEntity.getId()); 
		}catch(Exception e){
			XinLongUtil.xinLongUtil.printlog(log,e.getMessage());
			return 0;
		}
		   	
    };
    
    public List<SysEntity> findAll(){
    	String sql = "SELECT * FROM tbl_sys";       
    	return jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(SysEntity.class));
    };
    
    public String findByName(String name){
    	String sql = "SELECT Sysvalue FROM tbl_sys WHERE Name='" + name + "'";
        try{
        	return  jdbcTemplate.queryForObject(sql, String.class);
        }catch(Exception e){
        	XinLongUtil.xinLongUtil.printlog(log,e.getMessage());
        	return "";
        }
    	
    }
    
    public SysEntity findByEntityName(String name){
    	String sql = "SELECT * FROM tbl_sys WHERE Name='" + name + "'";
        try{
        	return  jdbcTemplate.queryForObject(sql, BeanPropertyRowMapper.newInstance(SysEntity.class));
        }catch(Exception e){
        	XinLongUtil.xinLongUtil.printlog(log,e.getMessage());
        	return null;
        }
    	
    }
    
    /*===============================================tbl_user_log=========================================================*/
    public Date getlastlogDate(){
    	String sql = "SELECT createtime FROM tbl_user_log order by createtime DESC Limit 1";
        try{
        	return  jdbcTemplate.queryForObject(sql, Date.class);
        }catch(Exception e){
        	XinLongUtil.xinLongUtil.printlog(log,e.getMessage());
        	return (new Date());
        }
    }
    
    public List<UserEntity> findUserAll(){
    	String sql = "SELECT * FROM tbl_users WHERE Username != 'hzwlprevail'";
     
    	return jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(UserEntity.class));
    }

    
}
